package brickdestroyer.simulation;

import brickdestroyer.math.Circle2D;
import brickdestroyer.math.DynamicShape2D;
import brickdestroyer.math.Vector2D;
import brickdestroyer.util.ImageResources;
import java.awt.Graphics;
import java.awt.Image;

/**
 * This class represents a ball in the game.
 * <p>It is esentially a circular shape with
 * dynamics and rendering attached to it.
 * 
 * @author Momchil Atanasov
 */
public class Ball extends Circle2D implements DynamicShape2D {

    /**
     * This constant represents the defaulr radius that balls in the
     * game will have.
     */
    public static final float DEFAULT_RADIUS = 13.5f;
   
    protected final Vector2D speed = new Vector2D();

    private final Image image = ImageResources.getImage("ball.png");
    
    public Ball() {
        this(0.0f, 0.0f, DEFAULT_RADIUS);
    }
    
    public Ball(float x, float y, float radius) {
        super(x, y, radius);
    }
    
    public Ball(Ball other) {
        this(other.x, other.y, other.radius);
        speed.setTo(other.speed);
    }

    public final Vector2D getSpeed() {
        return speed;
    }

    public float getMovability() {
        return 1.0f;
    }

    /**
     * This method limits the speed of the ball to a
     * maximum value as specified by the PhysicsManager
     * @see PhysicsManager
     */
    public void limitSpeed() {
        if (speed.getLength() > PhysicsManager.MAX_OBJECT_SPEED) {
            speed.setLength(PhysicsManager.MAX_OBJECT_SPEED);
        }
    }

    /**
     * This method displays the ball on the screen.
     * @param g interface to the graphics handle.
     */
    public void display(Graphics g) {
        int ballX = (int)(x - radius);
        int ballY = (int)(y - radius);
        int ballWidth = (int)(getRadius() * 2.0f);
        int ballHeight = (int)(getRadius() * 2.0f);
        g.drawImage(image, ballX, ballY, ballWidth, ballHeight, null);
    }
}
